package com.itheima.web.service.impl.oms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.result.PageResult;
import com.itheima.service.oms.OmsOrderReturnApplyService;
import com.itheima.vo.oms.OmsOrder;
import com.itheima.vo.oms.OmsOrderReturnApply;
import com.itheima.web.mapper.oms.OmsOrderReturnApplyMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

@DubboService
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply>
                                            implements OmsOrderReturnApplyService {
    @Autowired
    private OmsOrderReturnApplyService omsOrderReturnApplyService;

    @Override
    public PageResult listPage(String createTime, String handleMan, String handleTime,
                               Integer id, Integer pageNum, Integer pageSize, String receiverKeyword,Integer status) {
        Page<OmsOrderReturnApply> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrderReturnApply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(handleMan != null,OmsOrderReturnApply::getHandleMan,handleMan)
                .like(createTime != null,OmsOrderReturnApply::getCreateTime,createTime)
                .like(handleTime != null,OmsOrderReturnApply::getHandleTime,handleTime)
                .eq(id != null,OmsOrderReturnApply::getId,id)
                .eq(status != null,OmsOrderReturnApply::getStatus,status)
                .like(receiverKeyword != null,OmsOrderReturnApply::getReturnName,receiverKeyword)
                .like(receiverKeyword != null,OmsOrderReturnApply::getReturnPhone,receiverKeyword)
                .orderByAsc(OmsOrderReturnApply::getId);
        this.page(pageInfo,lqw);
        PageResult result = PageResult.init(pageInfo.getTotal(), pageNum.longValue(), pageSize.longValue(), pageInfo.getRecords());
        return result;
    }

    @Override
    public Boolean updateStatus(Integer id, OmsOrderReturnApply orderReturnApply) {
        orderReturnApply.setId(id.longValue());
        orderReturnApply.setHandleTime(LocalDateTime.now());
        if (orderReturnApply.getStatus() == 2){
            orderReturnApply.setReceiveTime(LocalDateTime.now());
        }
        return omsOrderReturnApplyService.updateById(orderReturnApply);
    }
}
